package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.OnlinePaymentFlowDao;
import com.xbongbong.saas.domain.entity.OnlinePaymentFlowEntity;
import com.xbongbong.saas.model.OnlinePaymentFlowModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

;

/**
 * @author bomfish
 */
@Service("onlinePaymentFlowModel")
public class OnlinePaymentFlowModelImpl implements OnlinePaymentFlowModel {
    @Resource
    private OnlinePaymentFlowDao onlinePaymentFlowDao;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Integer insert(OnlinePaymentFlowEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer success = onlinePaymentFlowDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_ONLINE_PAYMENT_FLOW);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<OnlinePaymentFlowEntity> list) {
        onlinePaymentFlowDao.insertBatch(list);
    }

    @Override
    public List<OnlinePaymentFlowEntity> findEntities(Map<String, Object> param) {
        return onlinePaymentFlowDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return onlinePaymentFlowDao.getEntitiesCount(param);
    }

    @Override
    public Integer update(OnlinePaymentFlowEntity entity) throws XbbException {
        Integer success = onlinePaymentFlowDao.update(entity);
        if (Objects.equals(1, success)) {
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_ONLINE_PAYMENT_FLOW);
            paasEsModel.update(insertDTO);
        }
        return success;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return onlinePaymentFlowDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return onlinePaymentFlowDao.getEntitiesCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return onlinePaymentFlowDao.findEntities(param);
    }

    @Override
    public OnlinePaymentFlowEntity getByKey(Long key, String corpid) {
        return onlinePaymentFlowDao.getByKey(key, corpid);
    }

}
